package com.shop.bussiness;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.g3net.database.DataBase;
import com.g3net.database.DataBaseFactory;
import com.g3net.database.DataBaseSet;
import com.g3net.tool.CollectionUtils;

/**
 * 用于完成各种数据的查询
 * @author yuxiaowei
 *
 */
public class DataQuery {
	private static Logger log = Logger.getLogger(DataQuery.class);
	/**
	 * 返回所有样式数据
	 * @return
	 */
	public static Map<String,CSData> queryCS()throws Exception
	{
		String sql="select * from cs ";
//		sql.append("order by ta.id desc");
		DataBase db=DataBaseFactory.getDataBase();
		List<CSData> list=db.doQueryClass(CSData.class, sql.toString(), null);
		Map<String,CSData> csdatas=new HashMap<String, CSData>();
		for (int i = 0; i < list.size(); i++) {
			String key=list.get(i).getWidth()+"_"+list.get(i).getHeight()+"_"+list.get(i).getMem();
			csdatas.put(key, list.get(i));
		}
		return csdatas;
	}
	public static Map<String,TempleteData> queryTemplete() throws Exception
	{
		String sql="select * from templetes ";
//		sql.append("order by ta.id desc");
		DataBase db=DataBaseFactory.getDataBase();
		List<TempleteData> list=db.doQueryClass(TempleteData.class, sql.toString(), null);
		Map<String,TempleteData> datas=new HashMap<String, TempleteData>();
		for (int i = 0; i < list.size(); i++) {
//			key:cs_pver_funid
			String key=list.get(i).getCs()+"_"+list.get(i).getPver()+"_"+list.get(i).getFunid();
			datas.put(key, list.get(i));
		}
		return datas;
	}
	public static Map<String,String> querySysinfo()throws Exception
	{
		String sql="select `key`,`value` from sysinfo ";
//		sql.append("order by ta.id desc");
		DataBase db=DataBaseFactory.getDataBase();
		DataBaseSet ds=db.doQuery(sql.toString(), null);
		
		Map<String,String> datas=new HashMap<String, String>();
		while(ds.next())
		{
			String k=ds.getString(1);
			String v=ds.getString(2);
			datas.put(k, v);
		}
		return datas;
	}
	public static List<Integer> queryWidths()throws Exception
	{
		String sql="select distinct(width) from cs order by width asc ";
//		sql.append("order by ta.id desc");
		DataBase db=DataBaseFactory.getDataBase();
		DataBaseSet ds=db.doQuery(sql.toString(), null);
		List<Integer> datas=new ArrayList<Integer>();
		while(ds.next())
		{
			datas.add(ds.getInt(1));
		}
		return datas;
	}
	public static List<Integer> queryHeights()throws Exception
	{
		String sql="select distinct(height) from cs order by width asc ";
//		sql.append("order by ta.id desc");
		DataBase db=DataBaseFactory.getDataBase();
		DataBaseSet ds=db.doQuery(sql.toString(), null);
		List<Integer> datas=new ArrayList<Integer>();
		while(ds.next())
		{
			datas.add(ds.getInt(1));
		}
		return datas;
	}
	public static List<Integer> queryMems()throws Exception
	{
		String sql="select distinct(mem) from cs order by width asc ";
//		sql.append("order by ta.id desc");
		DataBase db=DataBaseFactory.getDataBase();
		DataBaseSet ds=db.doQuery(sql.toString(), null);
		List<Integer> datas=new ArrayList<Integer>();
		while(ds.next())
		{
			datas.add(ds.getInt(1));
		}
		return datas;
	}
	public static void main(String[] args)throws Exception {
		long start=System.currentTimeMillis();
		Map<String,CSData> d=queryCS();
		System.out.println(d.size());
		System.out.println(CollectionUtils.toString(d));
		
		Map<String,TempleteData> d1=queryTemplete();
		System.out.println(d1.size());
		System.out.println(CollectionUtils.toString(d1));
		
		Map<String,String> d2=querySysinfo();
		System.out.println(d2.size());
		System.out.println(CollectionUtils.toString(d2));
		
		
		List<Integer> d3=queryWidths();
		System.out.println(d3.size());
		System.out.println(CollectionUtils.toString(d3));
		
		d3=queryHeights();
		System.out.println(d3.size());
		System.out.println(CollectionUtils.toString(d3));
		
		d3=queryMems();
		System.out.println(d3.size());
		System.out.println(CollectionUtils.toString(d3));
		long end=System.currentTimeMillis();
		System.out.println("times="+(end-start));
	}

}
